#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_txt_word_count.py
# author:张世航
# datetime:2024/11/9 17:44
# software: PyCharm
"""
this is function  description 
"""
# import module your need

# 6  对一篇英文小说，进行词频统计，输出前20个出现频率最高的单词；
#         英文小说"老人与海"     链接：https://pan.baidu.com/s/1goqK3zF8VBUFuD_2ezfZ7Q   提取码：mv04
#     提示：
#     1 可以先定义一个函数，专门来处理英文文章的读取问题；读取时，解决大小写问题，以及标点符号问题（如，将标点符号都替换成空格）；
#
from collections import Counter

def get_text(file_path):
    """读取文本文件并处理大小写和标点符号"""
    try:
        with open(file_path, "r", encoding="utf-8") as file:
            txt = file.read()
            txt = txt.lower()  # 转换为小写
            for ch in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~':
                txt = txt.replace(ch, " ")  # 替换标点符号为空格
            return txt
    except Exception as e:
        print(f"发生错误: {e}")
        return ""

def count_words(text):
    """统计单词频率并返回前20个频率最高的单词"""
    words = text.split()  # 按空格分割成单词
    word_counts = Counter(words)  # 统计单词频率
    return word_counts.most_common(20)  # 返回前20个单词及其频率

def main():

    file_path = "《老人与海》[英文版].txt"
    text = get_text(file_path)  # 读取并处理文本
    top_words = count_words(text)  # 统计词频
    print("前20个出现频率最高的单词：")
    for word, count in top_words:
        print(f"{word}: {count}")

if __name__ == "__main__":
    main()